package com.spica.spicaschool.ui.sport

import android.graphics.Color
import android.graphics.Typeface
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.github.mikephil.charting.components.Legend
import com.github.mikephil.charting.data.PieData
import com.github.mikephil.charting.data.PieDataSet
import com.github.mikephil.charting.data.PieEntry
import com.github.mikephil.charting.formatter.ValueFormatter
import com.spica.spicaschool.R
import com.spica.spicaschool.databinding.FragmentSportStepBinding


/**
 * 计步模式
 */
class SportStepFragment : Fragment() {

  private lateinit var viewBinding: FragmentSportStepBinding

  override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
  ): View {
    viewBinding = FragmentSportStepBinding.inflate(layoutInflater, container, false)
    initView()
    return viewBinding.root
  }

  private fun initView() {
    initDailyChartsTheme()

    val entries = listOf(
      PieEntry(3F, "运动"),
      PieEntry(8F, "休息"),
      PieEntry(8F, "睡眠"),
      PieEntry(5F, "其他")
    )

    val colors = listOf(
      Color.parseColor("#f5222d"),
      Color.parseColor("#fa8c16"),
      Color.parseColor("#a0d911"),
      Color.parseColor("#1890ff")
    )
    initDailyChartsData(
      entries,
      colors,
      requireContext().getColor(R.color.translucent_background)
    )
    initPlanChartTheme()
    val entries2 = listOf(
      PieEntry(72F, "完成"),
      PieEntry(28F, "未完成"),
    )

    val colors2 = listOf(
      Color.parseColor("#13c2c2"),
      Color.parseColor("#FFFFFF"),

      )
    initPlanChartsData(
      entries2,
      colors2,
      requireContext().getColor(R.color.translucent_background)
    )
  }


  private fun initDailyChartsTheme() {
    viewBinding.chartsDailyActivity.apply {
      holeRadius = 72F
      transparentCircleRadius = 78f
      centerText = "5\n小时"
      setCenterTextSize(22F)
      setTransparentCircleColor(Color.WHITE)
      setTransparentCircleAlpha(125)
      isRotationEnabled = false
      setCenterTextTypeface(Typeface.DEFAULT_BOLD)
      setUsePercentValues(true)
      legend.isEnabled = true//比例图
      legend.orientation = Legend.LegendOrientation.VERTICAL
      legend.verticalAlignment = Legend.LegendVerticalAlignment.TOP
      legend.horizontalAlignment = Legend.LegendHorizontalAlignment.RIGHT
      legend.textSize = 15F
      description.isEnabled = false
    }
  }


  private fun initPlanChartTheme() {
    viewBinding.chartsPlan.apply {
      holeRadius = 86F
      transparentCircleRadius = 90f
      centerText = "72%"
      setCenterTextSize(22F)
      setTransparentCircleColor(Color.WHITE)
      setTransparentCircleAlpha(125)
      isRotationEnabled = false
      setUsePercentValues(true)
      legend.isEnabled = false//比例图
      description.isEnabled = false
    }
  }


  private fun initDailyChartsData(
    pieEntries: List<PieEntry>,
    colors: List<Int>,
    lableColor: Int
  ) {
    val pieDataSet = PieDataSet(pieEntries, "")
    pieDataSet.colors = colors
    pieDataSet.selectionShift = 10f //设置选中后弹出的比例
    pieDataSet.sliceSpace = 0f //设置间距
    pieDataSet.valueTextColor = lableColor //设置显示百分比数据的颜色
    pieDataSet.valueTextSize = 16f //设置显示百分比数据的大小
    pieDataSet.isValueLineVariableLength = true
    pieDataSet.sliceSpace = 3f//间隙
    pieDataSet.valueLineColor = lableColor
    pieDataSet.valueLinePart1Length = 0.4f
    pieDataSet.valueLinePart2Length = 0.2f
    pieDataSet.yValuePosition = PieDataSet.ValuePosition.OUTSIDE_SLICE
    pieDataSet.xValuePosition = PieDataSet.ValuePosition.OUTSIDE_SLICE
    pieDataSet.values = pieEntries
    viewBinding.chartsDailyActivity.setEntryLabelColor(lableColor)
    pieDataSet.valueFormatter = object : ValueFormatter() {
      override fun getFormattedValue(value: Float): String {
        return "${(value / 24).toInt()}%"
      }
    }
    val pieData = PieData(pieDataSet)
    viewBinding.chartsDailyActivity.data = pieData
  }

  private fun initPlanChartsData(
    pieEntries: List<PieEntry>,
    colors: List<Int>,
    lableColor: Int
  ) {
    val pieDataSet = PieDataSet(pieEntries, "")
    pieDataSet.colors = colors
    pieDataSet.selectionShift = 10f //设置选中后弹出的比例
    pieDataSet.sliceSpace = 0f //设置间距
    pieDataSet.valueTextColor = lableColor //设置显示百分比数据的颜色
    pieDataSet.valueTextSize = 16f //设置显示百分比数据的大小
    pieDataSet.isValueLineVariableLength = true
    pieDataSet.sliceSpace = 3f//间隙
    pieDataSet.valueLineColor = lableColor
    pieDataSet.valueLinePart1Length = 0.4f
    pieDataSet.valueLinePart2Length = 0.2f
    pieDataSet.yValuePosition = PieDataSet.ValuePosition.OUTSIDE_SLICE
    pieDataSet.xValuePosition = PieDataSet.ValuePosition.OUTSIDE_SLICE
    pieDataSet.values = pieEntries
    viewBinding.chartsPlan.setEntryLabelColor(lableColor)
    pieDataSet.valueFormatter = object : ValueFormatter() {
      override fun getFormattedValue(value: Float): String {
        return "${(value / 24).toInt()}%"
      }
    }
    val pieData = PieData(pieDataSet)
    viewBinding.chartsPlan.data = pieData
  }
}
